package cn.hengzhu.main_manager.utils.filter;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;

import cn.hengzhu.main_manager.utils.config.gzh.AliConst;


/**
 * @author 刘成
 * @since 2018年8月6日 支付宝请求拦截
 * 
 */
// @WebFilter("/*") // 所有地址都能拦截
public class AliAuthFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest hRequest = (HttpServletRequest) request;
		HttpServletResponse hResponse = (HttpServletResponse) response;
		String fromUserName = (String) hRequest.getSession().getAttribute("fromUserName");
		if (StringUtils.isBlank(fromUserName)) {
			String agent = hRequest.getHeader("User-Agent");
			// 只有在支付宝端才会做的操作
			if (agent != null && agent.toLowerCase().indexOf("alipayclient") >= 0) {
				AlipayClient alipayClient = new DefaultAlipayClient(AliConst.URL, AliConst.APPID,
						AliConst.APP_PRIVATE_KEY, AliConst.FORMAT, AliConst.CHARSET, AliConst.ALIPAY_PUBLIC_KEY,
						AliConst.SIGN_TYPE);
				String code = request.getParameter("auth_code");

				if (code != null) {
					AlipaySystemOauthTokenRequest aliRequest = new AlipaySystemOauthTokenRequest();
					aliRequest.setCode(code);
					aliRequest.setGrantType("authorization_code");
					try {
						AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(aliRequest);
						hRequest.getSession().setAttribute("fromUserName", oauthTokenResponse.getUserId());
						hRequest.getSession().setAttribute("source", "3");
					} catch (AlipayApiException e) {
						e.printStackTrace();
					}

				} else {
					String query = hRequest.getQueryString();
					List<String> asList = Arrays.asList(query.split("&"));
					for (String string : asList) {
						List<String> asList2 = Arrays.asList(string.split("="));
						hRequest.getSession().setAttribute(asList2.get(0), asList2.get(1));
					}

					// 发送用户同意的请求
					String url = AliConst.AUTH_CODE.replace("APPID", AliConst.APPID).replace("SCOPE", AliConst.SCOPE)
							.replace("ENCODED_URL", URLEncoder.encode(AliConst.REDIRECT_URI, "UTF-8"));

					hResponse.sendRedirect(url);
					return;
				}
			}
		}
		chain.doFilter(hRequest, hResponse);
	}

	@Override
	public void destroy() {

	}

}
